package com.vrsys.interceptor;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.vrsys.util.HttpLanguageUtil;

public class SessionLocaleInterceptor extends HandlerInterceptorAdapter {
    @Autowired
    protected LocaleResolver localeResolver;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws ServletException {
        String newLocale = HttpLanguageUtil.parseLocaleString(request.getHeader("accept-language"));
        localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
        return true;
    }
}
